package com.leaf.lowcode.controller;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.leaf.lowcode.entity.Fire.FireCotrolWater;
import com.leaf.lowcode.utils.RegularInstance;
import com.tencent.welink.iot.SubDeviceDTO;
import com.tencent.welink.iot.WelinkHttp;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;


/**
 * 消防用水
 */


@Component
@RabbitListener(queues="SMR1210_V2_queuetest")
public class SendDataXFWATER {
    @RabbitHandler
    public void sendWDate(JSONObject fireCotrolWater) {
        /**
         * 进入消防用水
         */
        System.out.println("fireCotrolWater"+fireCotrolWater);
        List<SubDeviceDTO> subDeviceDTOList = RegularInstance.subDeviceDTOList;
        if (subDeviceDTOList != null) {
            for (SubDeviceDTO dto : subDeviceDTOList) {
                String pid = dto.getPid();
                String sn = dto.getSn();
                String din = dto.getDin();
                if(fireCotrolWater.get("deviceCode").equals(sn))
                {

                    System.out.println("找到设备"+sn);
                    if(fireCotrolWater.get("dataType").toString().equals("REAL_DATA"))
                    {
                        JSONObject standardData = new JSONObject();
                        standardData.put("reportTs", System.currentTimeMillis());
                        JSONObject profile = new JSONObject();
                        profile.put("modelId", 200608);
                        profile.put("poiCode", "w0307015");
                        standardData.put("profile", profile);
                        WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
                        JSONObject properties = new JSONObject();
                        JSONObject propertie = new JSONObject();
                        propertie.put("deviceType", fireCotrolWater.get("deviceType"));
                        propertie.put("indexCode", fireCotrolWater.get("deviceCode"));
                        propertie.put("dataType", fireCotrolWater.get("dataType"));
                        HashMap<String, Double> data = (HashMap<String, Double>) fireCotrolWater.get("data");
                        propertie.put("channel1", data.get("channel1"));
                        propertie.put("channel2", data.get("channel2"));
                        propertie.put("channel1Type", data.get("channel1Type"));
                        propertie.put("channel2Type", data.get("channel2Type"));
                        propertie.put("batteryValue", data.get("batteryValue"));
                        propertie.put("signal", data.get("signal"));
                        properties.put("smr1210v22", propertie);
                        standardData.put("properties", properties);
                        standardData.put("events", new JSONObject());
                        System.out.println(propertie);
                try {
                    System.out.println("消防用水");
                    String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
                wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong(din), devRunEvent, true);
                    System.out.println("消防用水"+devRunEvent);
                } catch (Exception e) {
                }

                    }else
                    {
                     //todo
                    }

                }
            }
        }
    }
}